package com.kelly.excel;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

/**
 * 往文件中写入数据
 * @author guofangbi
 *
 */
public class Write2File {

	/**
	 * 往指定文件中写入数据
	 * @param filePath 要写的文件绝对路径
	 * @param contentList 按行写入内容。建议在调用本方法之前，将数据按行拼接。
	 */
	public static void writeToFile(String filePath,List<String> contentList){
		BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(new File(filePath)));

            for (String line : contentList) {
            	 bw.write(line);// 这里是按行写入
            	 bw.newLine();
			}
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
	}
	
	public static void writeToExcel(List<?> list,String[] attrArr,String filePath) throws FileNotFoundException{
		FileOutputStream fileOutputStream = new FileOutputStream(filePath);
		HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        int rowNumber = 0;
        for (Object object : list) {
            Row row = sheet.createRow(rowNumber);
            for (int i = 0; i < attrArr.length; i++) {
                try {
                    row.createCell(i).setCellValue(BeanUtils.getSimpleProperty(object, attrArr[i]));
                } catch (Exception e) {
                	e.printStackTrace();
                }
            }
            rowNumber++;
        }
        try {
            workbook.write(fileOutputStream);
        } catch (IOException e) {
           e.printStackTrace();
        }finally {
            if (workbook!=null){
                try {
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
	}
	public static void main(String[] args) {
		List<String> contentList = new ArrayList<>(3);
		contentList.add("姓名,性别,爱好");
		contentList.add("张三,男,羽毛球");
		contentList.add("李四,女,乒乓球");
		
		Write2File.writeToFile("C:/Users/guofangbi/Desktop/test.csv", contentList);
		List<User> userList = new ArrayList<>();
		User user0 = new User("bizi0","12340");
		User user1 = new User("bizi1","12341");
		userList.add(user0);
		userList.add(user1);
		String[] attrArr = {"username","password"};
		try {
			Write2File.writeToExcel(userList, attrArr, "C:/Users/guofangbi/Desktop/test.xls");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	
}
